﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using RentAWorker.Business.Interfaces;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;
using System.Collections.ObjectModel;
using RentAWorker.Desktop.Entities;
using System.ComponentModel;
using RentAWorker.Management.Infrastructure;

namespace WorkerManagement.WorkerList
{
    public class WorkerListPresentationModel : IWorkerListPresentationModel, INotifyPropertyChanged,
                                                IHeaderInfoProvider<string>, IClosingAware
    {
        private IUnityContainer Container { get; set; }

        private IManageResourceService ResourceService { get; set; }

        public ICommand SearchCommand { get; set; }

        public ObservableCollection<Worker> WorkerList { get; set; }

        public WorkerListPresentationModel(IUnityContainer container, IWorkerListView view,
                                            IManageResourceService service)
        {
            this.Container = container;
            this.View = view;
            this.View.Model = this;

            this.ResourceService = service;

            this.WorkerList = new ObservableCollection<Worker>();
            this.SearchCommand = new DelegateCommand<string>(SearchWorkerByLocation);

            this.HeaderInfo = "Worker List";
        }

        private void SearchWorkerByLocation(string searchString)
        {
            this.WorkerList.Clear();

            foreach (RentResource item in
                this.ResourceService.GetResourcesByLocation(searchString, ResourceType.Worker))
            {
                this.WorkerList.Add((Worker)item);
            }
        }

        #region IWorkerListPresentationModel Members

        public IWorkerListView View { get; set; }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        #region IHeaderInfoProvider<string> Members

        string _header = default(string);

        public string HeaderInfo
        {
            get
            {
                return _header;
            }
            set
            {
                _header = value;
                OnPropertyChanged("Header");
            }
        }

        #endregion

        #region IClosingAware Members

        public bool RequestClose()
        {
            return this.CanClose;
        }

        public bool CanClose
        {
            get { return true; }
        }

        #endregion
    }
}
